﻿/**********************************************************\
*  TinyLib - Sprite                                        *
*                                                          *
*  Non-animated sprites.                                   *
*                                                          *
* (c) Jens Berglind                                        *
*                                                          *
* Last edited by Jens Berglind 25-06-2012                  *
\**********************************************************/ 

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TinyLib.Sprite
{
    public class Sprite : Node
    {
        public float rotation = 0.0f;
        public Color color = Color.White;

        protected Texture2D texture = null;

        public float layerDepth = 0.0f;
        public SpriteEffects effect = SpriteEffects.None;

        protected Rectangle sourceRectangle;

        public Texture2D Texture
        {
            get { return texture; }
        }

        public Rectangle SourceRectangle
        {
            get { return sourceRectangle; }
            set
            {
                sourceRectangle = value;

                if (value.X < 0) { sourceRectangle.X = 0; }
                if (value.Y < 0) { sourceRectangle.Y = 0; }
                if (value.Width > texture.Width) { sourceRectangle.Width = texture.Width; }
                if (value.Height > texture.Height) { sourceRectangle.Height = texture.Height; }
            }
        }

        public Sprite(SpriteBatch spriteBatch, Texture2D texture, Game game)
            : base(spriteBatch, game)
        {
            this.texture = texture;
            if (this.texture != null)
            {
                sourceRectangle.X = 0;
                sourceRectangle.Y = 0;
                sourceRectangle.Width = texture.Width;
                sourceRectangle.Height = texture.Height;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (Visible)
            {
                spritebatch.Draw(texture, position, sourceRectangle, color, rotation, origin, scale, effect, layerDepth);
            }
        }
    }
}
